<?php
class Agitum_Controller_Action extends Zend_Controller_Action
{
    
    public function init() 
    {
        $controller = $this->getRequest()->getControllerName();
    	$this->getHelper('ViewRenderer')->setViewScriptPathSpec(':controller/:action.:suffix');
    }

	public function preDispatch()
	{
	    return;
	    
		$request = $this->getRequest();
		
		$controller = $request->getControllerName();
		$action = $request->getActionName();
		$module = $request->getModuleName();
		
		if ( $module == 'default' && $controller == 'index' && $action == 'index' ) {
		    return;
		}
		
		$auth = Zend_Auth::getInstance();
		$role = ($auth->hasIdentity()) ? (string)$auth->getIdentity()->role : 'guest';
		
		$mvc = array('module'=>$module, 'controller'=>$controller, 'action'=>$action);
		$id = md5(implode('_', array_values($mvc)));
		
		$slot = new Agitum_Model_Cache_Slot_Page($id);
		$page = $slot->load();
		if ( !$page ) 
		{
		    $db = Zend_Registry::get('Zend_Db');
		    $page = Agitum_Model_Page::findByMVC($mvc);
		    if ( $page ) {
		         $slot->addTag(new Agitum_Model_Cache_Tag_Page($page->id));
		    }
		    $slot->save($page);
		}
		
		if ($page->name == '') return;
		
		$resource = $page->name;
		$privelege = $action;

		$acl = Zend_Registry::get('Zend_Acl');
		
		if( $acl->has($resource) && !$acl->isAllowed($role, $resource, $privelege) )
		{
		    $request->setDispatched(false);
		}
	}
}